import request from '@/utils/request'
import type { PageResult } from '@/types'

/**
 * 文件信息VO
 */
export interface FileInfoVO {
  id: number
  fileName: string
  fileUrl: string
  fileSize: number
  fileSizeFormatted: string
  fileType: string
  fileTypeName: string
  category: string
  categoryName: string
  auditStatus: number
  auditStatusName: string
  auditRemark?: string
  auditorName?: string
  auditTime?: string
  approvalRequired: number
  studentId?: number
  studentName?: string
  studentNo?: string
  uploaderName?: string
  createTime: string
  previewUrl?: string
  canPreview: boolean
}

/**
 * 文件上传参数
 */
export interface FileUploadParams {
  fileType?: string  // certificate-证书 medical-医疗证明 transfer-转学证明 id_card-身份证 photo-照片 academic-学籍材料 other-其他
  category?: string  // approval-审批附件 archive-档案附件 certificate-证书材料
  studentId?: number
  approvalId?: number
  approvalRequired?: number  // 0-不需要审核 1-需要审核
}

/**
 * 文件审核参数
 */
export interface FileAuditParams {
  fileId: number
  auditStatus: number  // 1-审核通过 2-审核驳回
  auditRemark?: string
}

/**
 * 上传单个文件
 * 注意：响应拦截器会自动解包 Result，直接返回 data 部分
 */
export function uploadFile(file: File, params?: FileUploadParams) {
  const formData = new FormData()
  formData.append('file', file)
  
  if (params?.fileType) formData.append('fileType', params.fileType)
  if (params?.category) formData.append('category', params.category)
  if (params?.studentId) formData.append('studentId', params.studentId.toString())
  if (params?.approvalId) formData.append('approvalId', params.approvalId.toString())
  if (params?.approvalRequired !== undefined) {
    formData.append('approvalRequired', params.approvalRequired.toString())
  }
  
  return request<FileInfoVO>({
    url: '/file/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 批量上传文件
 * 注意：响应拦截器会自动解包 Result，直接返回 data 部分
 */
export function uploadFiles(files: File[], params?: FileUploadParams) {
  const formData = new FormData()
  files.forEach(file => {
    formData.append('files', file)
  })
  
  if (params?.fileType) formData.append('fileType', params.fileType)
  if (params?.category) formData.append('category', params.category)
  if (params?.studentId) formData.append('studentId', params.studentId.toString())
  if (params?.approvalId) formData.append('approvalId', params.approvalId.toString())
  if (params?.approvalRequired !== undefined) {
    formData.append('approvalRequired', params.approvalRequired.toString())
  }
  
  return request<FileInfoVO[]>({
    url: '/file/upload/batch',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

/**
 * 分页查询文件列表
 */
export function getFilePage(params: {
  current: number
  size: number
  category?: string
  fileType?: string
  auditStatus?: number
  studentId?: number
}) {
  return request<PageResult<FileInfoVO>>({
    url: '/file/page',
    method: 'get',
    params
  })
}

/**
 * 获取我上传的文件
 */
export function getMyFiles(params: {
  current: number
  size: number
  category?: string
}) {
  return request<PageResult<FileInfoVO>>({
    url: '/file/my',
    method: 'get',
    params
  })
}

/**
 * 获取待审核文件列表
 */
export function getPendingAuditFiles(params: {
  current: number
  size: number
}) {
  return request<PageResult<FileInfoVO>>({
    url: '/file/pending-audit',
    method: 'get',
    params
  })
}

/**
 * 审核文件
 */
export function auditFile(data: FileAuditParams) {
  return request<void>({
    url: '/file/audit',
    method: 'post',
    data
  })
}

/**
 * 获取文件详情
 */
export function getFileDetail(id: number) {
  return request<FileInfoVO>({
    url: `/file/${id}`,
    method: 'get'
  })
}

/**
 * 删除文件
 */
export function deleteFile(id: number) {
  return request<void>({
    url: `/file/${id}`,
    method: 'delete'
  })
}

/**
 * 获取学生的所有文件
 */
export function getStudentFiles(studentId: number, category?: string) {
  return request<FileInfoVO[]>({
    url: `/file/student/${studentId}`,
    method: 'get',
    params: { category }
  })
}

